package chances.webui.taglibs;

import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;

import chances.webui.model.Input;
import chances.webui.model.Validator;

@SuppressWarnings("serial")
public class FormTag extends WebUITag{
	private String action;
	private String method;
	private String onsubmit;
	private boolean validate;
	private String enctype;
	private String target;
	private boolean upload ;
	private String  keyField;
	private boolean viewOnly;
	private boolean search; //是否是查詢的form
	private Map<String,Validator>  validators;
	private Map<String,Input>  fields;
	@Override
	public void setPageContext(PageContext pageContext) {
		validators = new LinkedHashMap<String,Validator>();
		fields = new LinkedHashMap<String,Input>();  
		super.setPageContext(pageContext);
	}
	
	@Override
	protected void reset() {
		this.action = null;
		this.method = null;
		this.onsubmit = null;
		this.validate = true;
		this.enctype = null;
		this.target = null;
		this.upload = false;
		this.viewOnly = false;
		this.search = false;
		
		super.reset();
	}
	@Override
	public int doEndTag() throws JspException {
		int result = super.doEndTag();
		this.validators.clear();
		return result;
	}

	@Override
	protected void initContext(Map<String, Object> context) {
		if(upload){
			this.enctype = "multipart/form-data";
		}
	}
	public void addValidator(String field, Validator vaildator) {
		this.validators.put(field, vaildator);
		
	}
	public Map<String, Validator> getValidators() {
		return validators;
	}
	
	public void addInput(Input input) {
		
		this.fields.put(input.getName(),input);
	}
	
	public Map<String, Input> getFields() {
		return fields;
	}
	
	public String getAction() {
		return action;
	}
	public String getMethod() {
		return method;
	}
	public String getOnsubmit() {
		return onsubmit;
	}
	public boolean isValidate() {
		return validate;
	}

	
	public void setAction(String action) {
		this.action = action;
	}
	public void setMethod(String method) {
		this.method = method;
	}
	public void setOnsubmit(String onsubmit) {
		this.onsubmit = onsubmit;
	}
	public void setValidate(boolean validate) {
		this.validate = validate;
	}
	
	public String getEnctype() {
		return enctype;
	}
	public void setEnctype(String enctype) {
		this.enctype = enctype;
	}
	public String getTarget() {
		return target;
	}
	public boolean isUpload() {
		return upload;
	}
	public void setTarget(String target) {
		this.target = target;
	}
	public void setUpload(boolean upload) {
		this.upload = upload;
	}

	public String getKeyField() {
		return keyField;
	}

	public void setKeyField(String keyField) {
		this.keyField = keyField;
	}

	public boolean isViewOnly() {
		return viewOnly;
	}

	public void setViewOnly(boolean viewOnly) {
		this.viewOnly = viewOnly;
	}

	public boolean isSearch() {
		return search;
	}

	public void setSearch(boolean search) {
		this.search = search;
	}
}
